package com.drinks.yiniang.ui.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.drinks.yiniang.R;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * @author han
 * @date 2019/11/21
 */
public class BubbleArrowView extends View {

    //画三角形的path
    private Path mPath = new Path();
    private Paint mPaint;
    //绘制圆角矩形
    private RectF mRectF = new RectF();

    private int mHeight;
    private int mWidth;
    private int mPaintWidth;
    private int mRectFMinHeight;
    //三角形的高
    private int mTriangleHeight;
    private int mRoundRadius;
    private int moveDis;

    public BubbleArrowView(Context context) {
        this(context,  null);
    }

    public BubbleArrowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BubbleArrowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        calculateHeight();
        init();
    }

    private void init() {
        mRectFMinHeight = ScreenSizeUtils.dp2px(getContext(), 30);
        mRoundRadius = ScreenSizeUtils.dp2px(getContext(), 3);
        moveDis = ScreenSizeUtils.dp2px(getContext(), 40);
        mTriangleHeight = ScreenSizeUtils.dp2px(getContext(), 10);
        mPaintWidth = 2;

        mPaint = new Paint();
        mPaint.setStrokeWidth(mPaintWidth);
        //mPaint.setColor(ColorUtils.getColorById(getContext(), R.color.color_f8f8f8));
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
    }

    /**
     * 测量宽度
     *
     * @param mode
     * @param width
     * @return
     */
    private int measureWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mWidth = width;
                break;
        }
        return mWidth;
    }

    /**
     * 测量高度
     *
     * @param mode
     * @param height
     * @return
     */
    private int measureHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mHeight = height;
                break;
        }

        return mHeight;
    }

    private void calculateHeight() {
        mHeight = mTriangleHeight + mPaintWidth + mRectFMinHeight;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawRoundRect(canvas);
        drawTriangle(canvas);
    }

    /**
     * 绘制圆角矩形
     *
     * @param canvas
     */
    private void drawRoundRect(Canvas canvas) {
        mRectF.set(0, mTriangleHeight + mPaintWidth, mWidth, mHeight);

        canvas.drawRoundRect(mRectF, mRoundRadius, mRoundRadius, mPaint);
    }

    /**
     * 绘制三角形
     * tipHeight + tipPaintWidth + triangleHeight + progressPaintWidth + progressMarginTop;
     *
     * @param canvas
     */
    private void drawTriangle(Canvas canvas) {
        moveDis = moveDis <= 0 ? 0 : moveDis + mTriangleHeight * 2 > mWidth ? mWidth - mTriangleHeight * 2 : moveDis;

        mPath.moveTo(moveDis, mTriangleHeight);
        mPath.lineTo(moveDis + mTriangleHeight / 2, 0);
        mPath.lineTo(moveDis + mTriangleHeight, mTriangleHeight);
        canvas.drawPath(mPath, mPaint);
        mPath.reset();
    }
}
